//
//  ViewController.m
//  PhotographVideoRecording
//
//  Created by 阿飞 on 2020/4/15.
//  Copyright © 2020 阿飞. All rights reserved.
//

#import "ViewController.h"
#import "SLShotViewController.h"
#import "CLPlayerView.h"

@interface ViewController ()

@property (nonatomic,weak) CLPlayerView *playerView;
@property (nonatomic,strong)NSURL *url;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self createButton];
    
    NSLog(@"12345667");
}
- (void)createButton{
    
       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       
       button.frame = CGRectMake(0, 100, 100, 100);
       
       button.backgroundColor = [UIColor redColor];
       
       [button setTitle:@"点我" forState:UIControlStateNormal];
       
       [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:button];
       
       UIButton *buttonVideo = [UIButton buttonWithType:UIButtonTypeCustom];
       
       buttonVideo.frame = CGRectMake(100, 100, 100, 100);
       
       buttonVideo.backgroundColor = [UIColor yellowColor];
       
       [buttonVideo setTitle:@"播放" forState:UIControlStateNormal];
       
       [buttonVideo addTarget:self action:@selector(buttonVideoClicked) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:buttonVideo];
}

-(void)buttonClicked{
    
    SLShotViewController * shotViewController = [[SLShotViewController alloc] init];
    shotViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    __weak typeof(self) weakSelf = self;
    //长按录视频获取url
    shotViewController.dismissViewControllerVideoURLPath = ^(NSURL * _Nonnull outputFileURL) {
        
        NSLog(@"长按录视频获取url%@",outputFileURL);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.url = outputFileURL;
    };
    //拍照及裁剪后获取照片
    shotViewController.dismissViewControllerImageClickData = ^(UIImage * _Nonnull image) {
        NSLog(@"获取拍照的照片%@",image);
    };
    //从相册选择照片输出数据
    shotViewController.dismissViewControllerAlbumImageData = ^(NSArray<UIImage *> * _Nonnull photos, NSArray * _Nonnull assets, BOOL isSelectOriginalPhoto) {
       NSLog(@"从相册选择照片输出照片%@",photos);
    };
    //从相册选择视频或者编辑过的视频
    shotViewController.dismissViewControllerAlbumVideoURLPath = ^(NSURL * _Nonnull outputFileURL) {
        NSLog(@"获取从相册选择视频或者编辑过的视频%@",outputFileURL);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.url = outputFileURL;
    };
    [self presentViewController:shotViewController animated:YES completion:nil];
    
}
-(void)buttonVideoClicked{
    
      CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)];
      _playerView = playerView;
     
      [self.view addSubview:_playerView];

      [_playerView updateWithConfigure:^(CLPlayerViewConfigure *configure) {
          //重复播放，默认不播放
          configure.repeatPlay = NO;
          //当前控制器是否支持旋转，当前页面支持旋转的时候需要设置，告知播放器
          configure.isLandscape = NO;
          //顶部工具条隐藏样式，默认不隐藏
          configure.topToolBarHiddenType = TopToolBarHiddenSmall;
      }];
      //视频地址
      _playerView.url = [NSURL fileURLWithPath:self.url.path];
      //播放
      [_playerView playVideo];
      //返回按钮点击事件回调,小屏状态才会调用，全屏默认变为小屏
      [_playerView backButton:^(UIButton *button) {
          NSLog(@"返回按钮被点击");
      }];
      //播放完成回调
      [_playerView endPlay:^{
          NSLog(@"播放完成");
      }];
}
-(void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    [_playerView destroyPlayer];
    
}
@end
